package hdikow;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

/**
 *
 * @author Heidi Dikow
 */
public class Utility {

    /**
     *
     * This method calculates the index of characters in the evaluation matrix
     * in order to be able to donate a score to two characters
     *
     * @param fileName the name of the text file containing the evaluation
     * matrix
     * @return a hashMap mapping characters to their index in the evaluation
     * matrix
     */
    public static HashMap<String, Integer> buildCharacterIndex(String fileName) {
        HashMap<String, Integer> characterIndex = new HashMap();
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(
                    new FileInputStream(file), "UTF-8"));
            String line;

            while ((line = reader.readLine()) != null) {
                String[] values = line.split("\\s+");
                for (int i = 0; i < values.length; i++) {
                    characterIndex.put(values[i], i);
                }
                break;
            }

        } catch (IOException ex) {
            Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
        }
        return characterIndex;
    }

    /**
     *
     * This method reads a text file and returns an integer matrix which
     * contains the values of this file
     *
     * @param fileName The name of the file to read from in order to put the
     * values of the file into a integer matrix
     * @return the integer matrix which contains the values of the read file.
     * The values represent the score of two characters
     */
    public static int[][] buildEvaluationMatrix(String fileName) {
        /*
         * as both evaluation matrices have the same size, the size of the 
         * matrix is hard coded here
         */
        int[][] evaluationMatrix = new int[21][21];
        int row = 0;
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(
                    new FileInputStream(file), "UTF-8"));
            //read first line because we want to start at the second
            reader.readLine();

            String line;
            while ((line = reader.readLine()) != null) {
                String[] values = line.split("\\s+");
                //start loop at second entry as we don't care for the characters
                for (int i = 1; i < values.length; i++) {
                    evaluationMatrix[row][i - 1] = Integer.valueOf(values[i]);
                }
                row++;
            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
        }

        return evaluationMatrix;
    }

    /**
     *
     * @param a first input String to check
     * @param b second input String to check
     * @return true if both strings are in single letter code format otherwise
     * false
     */
    public static boolean correctInput(String a, String b) {
        return Pattern.matches("[ARNDCQEGHILKMFPSTWYV*]*", a)
                && Pattern.matches("[ARNDCQEGHILKMFPSTWYV*]*", b);
    }
    
    /**
     * compares three values and renders the maximal value
     * @param x first value
     * @param y second value
     * @param z third value
     * @return  the maximum of the three values
     */
    public static int maxValue(int x, int y, int z) {
        int max = (int) Math.max(x, y);
        max = (int) Math.max(max, z);
        return max;
    }
}
